﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITM495.Lab2.Core;
using ITM495.Lab2.Core.Entities;
using System.IO;

namespace ITM495.Lab2.Impl.Writers
{
    public class TextWriter : IWriter<Tweet>
    {
        private String _lineFormat = "{0}\t{1}\t{2}\t{3}\t{4}\r\n";

        protected String Path { get; set; }

        public TextWriter(string path)
        {
            Path = path;
        }

        #region IWriter<Tweet> Members

        public void Write(IList<Tweet> entities)
        {
            if(File.Exists(Path))
                File.Delete(Path);

            using (var textFile = new StreamWriter(Path))
            {
                foreach(var t in entities)
                {
                    textFile.Write(_lineFormat,t.Title, t.Description, t.PubDate.ToString("d"), t.Guid, t.Link);
                }
            }
        }

        #endregion
    }
}
